﻿//------------------------------------------------------------------------------ 
// <copyright file="JModal.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.modal.css", "text/css")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.Images.close.png", "image/png")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.Images.hoverclose.png", "image/png")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.Images.max.png", "image/png")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.Images.maxhover.png", "image/png")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.Images.min.png", "image/png")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.Images.minhover.png", "image/png")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.Images.restore.png", "image/png")]
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JModal.Images.restorehover.png", "image/png")]

namespace FmjQuery
{
    using System;
    using System.ComponentModel;
    using System.Text;
    using System.Web.UI;

    /// <summary>
    /// Create Modal Window
    /// </summary>
    [ToolboxData("<{0}:JModal runat=server />")]
    [DefaultProperty("TargetControlID")]
    [NonVisualControl]
    public class JModal : JQueryExtendControl
    {
        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JModal class.
        /// </summary>
        public JModal()
        {
            this.IsRenderControl = false;
        }
        #endregion

        #region Property
        /// <summary>
        /// Gets or sets Title
        /// </summary>
        [Description("Title for modal form")]
        public string Title
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets Modal class
        /// </summary>
        [Description("Name of Modal class")]
        public string ModalCssClass
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets Content Class
        /// </summary>
        [Description("Name of Content class")]
        public string ContentCssClass
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets Icon Class
        /// </summary>
        [Description("Name of icon class")]
        public string IconCssClass
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets Name Modal Control
        /// </summary>
        [Description("Code of modal control")]
        public string ModalIDControl
        { 
            get; set;
        }

        /// <summary>
        /// Gets or sets setting for close button
        /// </summary>
        [Description("Close Button Detail (json format)")]
        public string CloseButtonDetail
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets setting for Minimized Button
        /// </summary>
        [Description("Minimized Button Detail (json format)")]
        public string MinimizedButtonDetail
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets setting for Maximized Button
        /// </summary>
        [Description("Maximized Button Detail (json format)")]
        public string MaximizedButtonDetail
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether Modal control is client
        /// </summary>
        [Description("Indicating if modal control is client")]
        public bool? ModalIsClient
        {
            get; set;
        }
        #endregion

        #region JQueryExtendControl
        /// <summary>
        /// Gets all resource need include for use this control
        /// </summary>
        /// <returns>list of resource</returns>
        public override JWebSources GetResources()
        {
            return new JWebSources { new JSource(Properties.Settings.Default.Css_Modal_Name, Properties.Settings.Default.Css_Modal_Url, JSourceType.css, true) };
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            this.Manager.ModalPopUp = true;

            if (this.Enabled)
            {
                bool isClientModal = this.ModalIsClient.HasValue ? this.ModalIsClient.Value : false;

                // check params
                if (string.IsNullOrEmpty(this.ModalIDControl) || (!isClientModal && FindControl(this.ModalIDControl) == null))
                {
                    throw new InvalidOperationException("You must set ModalIDControl");
                }

                ModalCloseButton close = string.IsNullOrEmpty(this.CloseButtonDetail) ? new ModalCloseButton { Visible = true } : Utility.DeserializeJSon<ModalCloseButton>(this.CloseButtonDetail);
                ModalMaxButton max = string.IsNullOrEmpty(this.MaximizedButtonDetail) ? new ModalMaxButton { Visible = true } : Utility.DeserializeJSon<ModalMaxButton>(this.MaximizedButtonDetail);
                ModalMinButton min = string.IsNullOrEmpty(this.MinimizedButtonDetail) ? new ModalMinButton { Visible = true } : Utility.DeserializeJSon<ModalMinButton>(this.MinimizedButtonDetail);

                // check button
                if (close.Visible)
                {
                    if (string.IsNullOrEmpty(close.Normal))
                    {
                        close.Normal = this.Page.GetImageResourceUrl(GetType(), Properties.Settings.Default.Image_Modal_Close);
                    }

                    if (string.IsNullOrEmpty(close.Hover))
                    {
                        close.Hover = this.Page.GetImageResourceUrl(GetType(), Properties.Settings.Default.Image_Modal_CloseHover);
                    }
                }

                if (max.Visible)
                {
                    if (string.IsNullOrEmpty(max.Normal))
                    {
                        max.Normal = this.Page.GetImageResourceUrl(GetType(), Properties.Settings.Default.Image_Modal_Max);
                    }

                    if (string.IsNullOrEmpty(max.Hover))
                    {
                        max.Hover = this.Page.GetImageResourceUrl(GetType(), Properties.Settings.Default.Image_Modal_MaxHover);
                    }

                    if (max.RestoreButton == null)
                    {
                        max.RestoreButton = new ModalRestoreButton();
                    }

                    if (string.IsNullOrEmpty(max.RestoreButton.Normal))
                    {
                        max.RestoreButton.Normal = this.Page.GetImageResourceUrl(GetType(), Properties.Settings.Default.Image_Modal_Restore);
                    }

                    if (string.IsNullOrEmpty(max.RestoreButton.Hover))
                    {
                        max.RestoreButton.Hover = this.Page.GetImageResourceUrl(GetType(), Properties.Settings.Default.Image_Modal_RestoreHover);
                    }
                }

                if (min.Visible)
                {
                    if (string.IsNullOrEmpty(min.Normal))
                    {
                        min.Normal = this.Page.GetImageResourceUrl(GetType(), Properties.Settings.Default.Image_Modal_Min);
                    }

                    if (string.IsNullOrEmpty(min.Hover))
                    {
                        min.Hover = this.Page.GetImageResourceUrl(GetType(), Properties.Settings.Default.Image_Modal_MinHover);
                    }
                }

                string clientIdModal = isClientModal ? this.ModalIDControl : FindControl(ModalIDControl).ClientID;
                StringBuilder builder = new StringBuilder();

                // open function
                builder.AppendFormat("function {0}_open()", TargetControlID);
                builder.Append("{");

                builder.AppendFormat("$.openModal('{0}',", clientIdModal);
                builder.Append("{");
                builder.AppendFormat("title: '{0}', modalCss: '{1}', contentCss: '{2}',", this.Title ?? "PopUp", this.ModalCssClass ?? "modal", this.ContentCssClass ?? "content");

                builder.Append("closeButton: {");
                builder.AppendFormat("visible: {0}, normal: '{1}', hover: '{2}'", close.Visible.ToString().ToLower(), close.Normal, close.Hover);
                builder.Append("},");

                builder.Append("maximizedButton: {");
                builder.AppendFormat("visible: {0}, normal: '{1}', hover: '{2}',", max.Visible.ToString().ToLower(), max.Normal, max.Hover);
                builder.Append("restored: {");
                builder.AppendFormat("normal: '{0}', hover: '{1}'", max.RestoreButton.Normal, max.RestoreButton.Hover);
                builder.Append("}},");

                builder.Append("minimizedButton: {");
                builder.AppendFormat("visible: {0}, normal: '{1}', hover: '{2}'", min.Visible.ToString().ToLower(), min.Normal, min.Hover);
                builder.Append("},");

                builder.AppendFormat("IconClass: '{0}'", this.IconCssClass ?? "reduce");
                builder.Append("});");

                builder.Append("}");

                // openScript
                builder.Append("$(document).ready(function(en){");
                builder.AppendFormat("$('#{0}').bind('click',function(e)", this.TargetControlClientID);
                builder.Append("{");
                builder.AppendFormat("{0}_open();return false;", TargetControlID);
                builder.Append("});});");

                // hidden modal
                builder.AppendFormat("$('#{0}').css('display','none');", clientIdModal);

                Utility.RegisterStartUpScript(Page, string.Concat("modal_", this.ClientID), builder.ToString());
            }
        }
        #endregion
    }
}
